angular.module('recharge', [])
    .controller('rechargeCtrl', ['$scope', 'httpService', 'alertmsg', '$stateParams', '$rootScope', '$timeout', function($scope, httpService, alertmsg, $stateParams, $rootScope, $timeout) {
        $scope.money = {
            pay_type: pay_type
        };
        
        if($stateParams.money) {
            console.log('$stateParams.money', $stateParams.money)
            $scope.money.price = (parseFloat($stateParams.money) - $rootScope.user.amount)/ 100;
        }

        // 设置支付类型
        $scope.setpaytype = function(type) {
            if(type == 'alipay_pc_direct' && isMobile) {
                $scope.money.pay_type = pay_type;
            } else {
                $scope.money.pay_type = type;
            }
        };
        
        // 充值
        $scope.recharge = function(recharge) {
            var money = angular.copy(recharge);
            money.price = money.price * 100;
            $scope.firstupdate = false;
            if(!$scope.firstupdate) {
                $scope.firstupdate = true;
                httpService.query('web/payments/recharge', money, 'post').then(function(res) {
                    $timeout(function() {$scope.firstupdate = false;}, 1000)
                    if (res.code == 200) {
                        pingpp.createPayment(res.charge, function(result, err) {
                            if (result == "success") {
                                alert(JSON.stringify({ 'success': result }));
                                $state.go('mine.list', { ctrl: 'money' });
                                alert('充值成功！');
                            } else if (result == "fail") {
                                alert(JSON.stringify({ 'fail': result }));
                                console.log("fail");
                            } else if (result == "cancel") {
                                console.log("cancel");
                                alert(JSON.stringify({ 'cancel': result }));
                            }
                        });
                    } else {
                        alertmsg(res.msg);
                    }
                }, function() {$scope.firstupdate = false;});
            } else {
                alertmsg('正在提交请稍等！');
            }
        };


        // function GetQueryString(name) {
        //     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        //     console.log('window.location.search.', window.location.search)
        //     var r = window.location.search.substr(1).match(reg);
        //     if (r != null) return unescape(r[2]);
        //     return null;
        // }
        // console.log("GetArgsFromHref(window.href, 'trade_status')", GetQueryString('trade_status'))
            // GetArgsFromHref(window.href, 'trade_status');
    }]);
